package com.dek.seckill.controller;

import com.dek.seckill.common.handler.AccessLimit;
import com.dek.seckill.common.mq.MQProducer;
import com.dek.seckill.common.redis.RedisService;
import com.dek.seckill.common.redis.UserKey;
import com.dek.seckill.common.util.Result;
import com.dek.seckill.entity.User;
import com.dek.seckill.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private MQProducer mqProducer;



    @RequestMapping("hello")
    public String hello(Model model){
        model.addAttribute("name", "dek");
        return "hello";
    }

    @RequestMapping("user/{id}")
    @ResponseBody
    public Result<User> getUserById(@PathVariable Integer id) {
        return Result.success(userService.getById(id));
    }


    @AccessLimit(seconds = 10, maxCount = 5, login = true)
    @RequestMapping("mq")
    @ResponseBody
    public Result mq() {
        mqProducer.produce("dek");
        return Result.success();
    }

    @RequestMapping("topicMq")
    @ResponseBody
    public Result topicMq() {
        mqProducer.produceTopic("dek");
        return Result.success();
    }


    @RequestMapping("fanoutMq")
    @ResponseBody
    public Result fanoutMq() {
        mqProducer.produceFanout("dek");
        return Result.success();
    }

    @RequestMapping("headerMq")
    @ResponseBody
    public Result headerMq() {
        mqProducer.produceHeader("dek");
        return Result.success();
    }

    @RequestMapping("redis/get")
    @ResponseBody
    public Result<User> user() {
        User v1 = redisService.get(UserKey.getById,"1", User.class);
        return Result.success(v1);
    }

    @RequestMapping("redis/set")
    @ResponseBody
    public Result<Boolean> setuser() {
        User user = userService.getById(1);
        redisService.set(UserKey.getById,"1", user);
        return Result.success(true);
    }

}
